﻿using System;
using System.Collections.Generic;
using Raven.Client;

namespace Rabbit.Logic
{
    public class CriticalLogic
    {

        public static string OrderSetShipment(IDocumentStore store, string orderId, string shopId, Shipment shipment)
        {
            OrderLog log = RabbitUse.MakeOrderLogReady(store, orderId);

            string result = string.Empty;
            using (IDocumentSession session = store.OpenSession()) 
            {
                Order order = session.Load<Order>(orderId);
                if (order.ShopId != shopId)
                {
                    result = "没有权限";
                    return result;
                }
                if (order.State == OrderState.Confirmed || order.State == OrderState.Canceled)
                {
                    result = "订单已完结，状态不可更改。";
                    return result;
                }

                string oldState = order.State.ToDescription();
                shipment.ShippedAt = DateTime.Now;
                order.ShipmentInfo = shipment;
                order.State = OrderState.Sent;
                session.Store(order);

                log.LogList.Add(string.Format("{0} 卖家 将订单状态由 {1} 变更为 {2}", DateTime.Now, oldState, order.State.ToDescription()));
                session.Store(log);

                session.SaveChanges();
            }
            return result;
        }

        public static string OrderSetCanceled(IDocumentStore store, string orderId, string shopId)
        {
            string result = string.Empty;

            OrderLog log = RabbitUse.MakeOrderLogReady(store, orderId);

            using (IDocumentSession session = store.OpenSession())
            {
                Order order = session.Load<Order>(orderId);
                if (order.ShopId != shopId)
                {
                    result = "没有权限";
                    return result;
                }

                if (order.State == OrderState.Confirmed || order.State == OrderState.Canceled)
                {
                    result = "订单已完结，状态不可更改。";
                    return result;
                }

                string oldState = order.State.ToDescription();
                order.State = OrderState.Canceled;
                session.Store(order);

                log.LogList.Add(string.Format("{0} 卖家 将订单状态由 {1} 变更为 {2}", DateTime.Now, oldState, order.State.ToDescription()));
                session.Store(log);

                session.SaveChanges();
            }
            return result;
        }

        public static string OrderSetPriceFinal(IDocumentStore store, string orderId, string shopId, decimal finalPrice)
        {
            string result = string.Empty;
            using (IDocumentSession session = store.OpenSession())
            {
                Order order = session.Load<Order>(orderId);
                if (order.ShopId != shopId)
                {
                    result = "没有权限";
                    return result;
                }

                if (order.State == OrderState.Confirmed || order.State == OrderState.Canceled)
                {
                    result = "订单已完结，状态不可更改。";
                    return result;
                }

                if (order.State != OrderState.WaitPayment)
                {
                    result = "订单已付款，价格不可更改。";
                    return result;
                }

                string old = order.FinalPrice.ToString();
                order.FinalPrice = finalPrice;
                session.Store(order);

                OrderLog log;
                if (string.IsNullOrEmpty(order.OrderLogId))
                {
                    log = new OrderLog();
                    log.OrderId = order.Id;
                    log.LogList = new List<string>();
                    session.Store(log);
                    order.OrderLogId = log.Id;
                    session.Store(order);
                }
                else
                {
                    log = session.Load<OrderLog>(order.OrderLogId);
                }
                log.LogList.Add(string.Format("{0} 订单总价（含运费）由 {1} 变更为 {2}", DateTime.Now, old, order.FinalPrice));
                session.Store(log);

                session.SaveChanges();
            }
            return result;
        }



    }
}
